<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入商品编号" v-model="listQuery.goodsSn">
      </el-input>
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入商品名称" v-model="listQuery.name">
      </el-input>
      <el-cascader clearable class="filter-item" style="width: 150px" placeholder="请选择商品类目" expand-trigger="hover" v-model="categoryIds" :options="categoryList" @change="handleCategoryChange">
      </el-cascader>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
      <el-button class="filter-item" type="primary" :loading="downloadLoading" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-badge :value="lowRepertory" :max="99" class="item">
        <el-button class="filter-item" type="warning" size="small" round style="margin-left:30px;" @click="queryLowRepertoryList">库存不足</el-button>
      </el-badge>
      <el-badge :value="highRepertory" :max="99" class="item">
        <el-button class="filter-item" type="warning" size="small" round style="margin-left:30px;" @click="queryHighRepertoryList">库存积压</el-button>
      </el-badge>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="宣传画廊">
              <img class="gallery" v-for="pic in props.row.gallery" :key="pic" :src="pic" />
            </el-form-item>
            <el-form-item label="商品介绍">
              <span>{{ props.row.brief }}</span>
            </el-form-item>
            <el-form-item label="商品单位">
              <span>{{ props.row.unit }}</span>
            </el-form-item>
            <el-form-item label="关键字">
              <span>{{ props.row.keyword }}</span>
            </el-form-item>
            <el-form-item label="类目ID">
              <span>{{ props.row.categoryId }}</span>
            </el-form-item>
            <el-form-item label="品牌商ID">
              <span>{{ props.row.brandId }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商品编号" prop="goodsSn">
      </el-table-column>

      <el-table-column align="center" min-width="100" label="名称" prop="name">
      </el-table-column>

      <el-table-column align="center" property="iconUrl" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="40" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="详情" prop="desc">
        <template slot-scope="scope">
          <el-dialog title="商品详情" :visible.sync="descDialogVisible">
            <div v-html="descDetail"></div>
          </el-dialog>
          <el-button type="primary" size="mini" @click="showDesc(scope.row.desc)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="库存" prop="counterPrice">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showRepetory(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="积分" min-width="100" prop="integral">
      </el-table-column>
      <el-table-column align="center" label="爱币" min-width="100" prop="retailPrice">
      </el-table-column>

      <el-table-column align="center" label="是否新品" prop="isNew">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isNew ? 'success' : 'error' ">{{scope.row.isNew ? '新品' : '非新品'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否热品" prop="isHot">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isHot ? 'success' : 'error' ">{{scope.row.isHot ? '热品' : '非热品'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否在售" prop="isOnSale">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isOnSale ? 'success' : 'error' ">{{scope.row.isOnSale ? '在售' : '未售'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 库存详情 -->
    <el-dialog title="库存详情" width="800" :visible.sync="productVisible">
      <el-form :data="productForm" label-position="left">
        <el-form-item>
          <el-table size="small" :data="productForm" border fit highlight-current-row>
            <el-table-column align="center" label="货品规格" prop="specifications" />
            <el-table-column align="center" label="库存数量" prop="number" />
            <el-table-column align="center" label="最小数量" prop="smallNumber" />
            <el-table-column align="center" label="最大数量" prop="bigNumber" />
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibilityHeight="100"></back-to-top>
    </el-tooltip>

  </div>
</template>

<style>
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 100px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}
.gallery {
  width: 80px;
  margin-right: 10px;
}
</style>

<script>
import { listGoods, listTotalGoods, deleteGoods, queryUnShipOrderNum, queryProductDetails, listCatAndBrand } from '@/api/goods'
import BackToTop from '@/components/BackToTop'

export default {
  name: 'GoodsList',
  components: { BackToTop },
  data() {
    return {
      lowRepertory: 0,
      highRepertory: 0,
      categoryList: [],
      list: [],
      total: 0,
      listLoading: true,
      categoryIds: [],
      listQuery: {
        page: 1,
        limit: 20,
        goodsSn: undefined,
        name: undefined,
        cId: undefined,
        sort: '+id',
        flag: 0
      },
      productVisible: false,
      productForm: [],
      descDetail: '',
      descDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
    this.queryUnShipOrderNum()
    this.init()
  },
  methods: {
    queryUnShipOrderNum() {
      queryUnShipOrderNum().then(response => {
        this.lowRepertory = response.data.data.lowTotal
        this.highRepertory = response.data.data.highTotal
      })
    },
    init: function() {
      listCatAndBrand().then(response => {
        this.categoryList = response.data.data.categoryList
      })
    },
    handleCategoryChange(value) {
      this.goods.categoryId = value[value.length - 1]
    },
    getList() {
      this.listLoading = true
      this.listQuery.cId = this.categoryIds[1]
      listGoods(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.listQuery.flag = 0
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/goods/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/goods/edit', query: { id: row.id }})
    },
    showDesc(desc) {
      this.descDetail = desc
      this.descDialogVisible = true
    },
    queryLowRepertoryList() {
      this.listQuery.flag = 1
      this.getList()
    },
    queryHighRepertoryList() {
      this.listQuery.flag = 2
      this.getList()
    },
    showRepetory(id) {
      queryProductDetails({ goodId: id }).then(response => {
        this.productForm = response.data.data.product
      })
      this.productVisible = true
    },
    handleDelete(row) {
      this.$confirm('此操作删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods(row).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      })
    },
    handleDownload() {
      import('@/vendor/Export2Excel').then(excel => {
        listTotalGoods(this.listQuery).then(response => {
          const tHeader = [
            '商品ID',
            '排列ID',
            '规格ID',
            '商品编号',
            '品牌商ID',
            '品牌商',
            '名称',
            '类目',
            '类目ID',
            '规格名称',
            '爱币售价',
            '积分售价',
            '单位重量/g',
            '货品数量',
            '爱币价格',
            '爱币类型',
            '积分类型',
            '专柜价格',
            '是否新品',
            '是否热品',
            '是否在售',
            '商品介绍',
            '商品单位',
            '关键字'
          ]
          const filterVal = [
            'id',
            'orderId',
            'ids',
            'goodsSn',
            'brandId',
            'brandName',
            'name',
            'categoryId',
            'categoryName',
            'specifications',
            'price',
            'integral',
            'weight',
            'number',
            'retailPrice',
            'priceFlag',
            'integralFlag',
            'counterPrice',
            'isNew',
            'isHot',
            'isOnSale',
            'brief',
            'unit',
            'keywords'
          ]
          const list = response.data.data.items
          for (var i = 0; i < list.length; i++) {
            if (list[i].isNew === true) list[i].isNew = '新品'
            else if (list[i].isNew === false) list[i].isNew = '非新品'

            if (list[i].priceFlag === 1) list[i].priceFlag = '扣除'
            else if (list[i].priceFlag === 2) list[i].priceFlag = '赠送'

            if (list[i].integralFlag === 1) list[i].integralFlag = '扣除'
            else if (list[i].integralFlag === 2) list[i].integralFlag = '赠送'

            if (list[i].isHot === true) list[i].isHot = '热品'
            else if (list[i].isHot === false) list[i].isHot = '非热品'

            if (list[i].isOnSale === true) list[i].isOnSale = '在售'
            else if (list[i].isOnSale === false) list[i].isOnSale = '未售'
          }
          excel.export_json_to_excel2(tHeader, list, filterVal, '商品信息')
          this.downloadLoading = false
        })
      })
    }
  }
}
</script>
